package com.hiultra.archive.service.impl;

import java.util.List;

import com.hiultra.archive.domain.ArcArchive;
import com.hiultra.archive.mapper.ArcAppraialDetailsMapper;
import com.hiultra.archive.mapper.ArcArchiveMapper;
import com.hiultra.common.constant.UserConstants;
import com.hiultra.common.exception.ServiceException;
import com.hiultra.common.utils.ArcUtil;
import com.hiultra.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

import com.hiultra.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hiultra.archive.domain.ArcAppraialDetails;
import com.hiultra.archive.mapper.ArcAppraisalMapper;
import com.hiultra.archive.domain.ArcAppraisal;
import com.hiultra.archive.service.IArcAppraisalService;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 鉴定单Service业务层处理
 *
 * @author ls
 * @date 2023-08-31
 */
@Service
public class ArcAppraisalServiceImpl implements IArcAppraisalService {
    @Autowired
    private ArcAppraisalMapper arcAppraisalMapper;
    @Autowired
    private ArcArchiveMapper arcArchiveMapper;
    @Autowired
    private ArcAppraialDetailsMapper arcAppraialDetailsMapper;


    /**
     * 查询鉴定单
     *
     * @param appraisalId 鉴定单主键
     * @return 鉴定单
     */
    @Override
    public ArcAppraisal selectArcAppraisalByAppraisalId(Long appraisalId) {
        return arcAppraisalMapper.selectArcAppraisalByAppraisalId(appraisalId);
    }

    /**
     * 查询鉴定单列表
     *
     * @param arcAppraisal 鉴定单
     * @return 鉴定单
     */
    @Override
    public List<ArcAppraisal> selectArcAppraisalList(ArcAppraisal arcAppraisal) {
        return arcAppraisalMapper.selectArcAppraisalList(arcAppraisal);
    }

    /**
     * 新增鉴定单
     *
     * @param arcAppraisal 鉴定单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertArcAppraisal(ArcAppraisal arcAppraisal) {
        int rows = 0;
        try {
            arcAppraisal.setCreateTime(DateUtils.getNowDate());
            // 数据判空
            if (StringUtils.isNull(arcAppraisal.getArcAppraialDetailsList())) {
                throw new ServiceException("鉴定明细为空，请检查重试！");
            }
            //生成单号以及其他信息
            arcAppraisal.setAppraisalNo("JD" + ArcUtil.getOrderNo());
            arcAppraisal.setAppraisalTime(DateUtils.getNowDate());
            //循环处理子表明细同时处理各种状态
            rows = arcAppraisalMapper.insertArcAppraisal(arcAppraisal);
            for (ArcAppraialDetails arcAppraialDetails : arcAppraisal.getArcAppraialDetailsList()) {
                //修改档案信息
                ArcArchive archive = new ArcArchive();
                archive.setArchiveId(arcAppraialDetails.getArchiveId());
                archive.setExpireTime(arcAppraialDetails.getAppraisalAfterTime());
                arcArchiveMapper.updateArcArchive(archive);
            }

            insertArcAppraialDetails(arcAppraisal);
            // 提交事务
            return rows;
        } catch (Exception e) {
            // 手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw e;
        }
    }

    /**
     * 修改鉴定单
     *
     * @param arcAppraisal 鉴定单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateArcAppraisal(ArcAppraisal arcAppraisal) {
        arcAppraisalMapper.deleteArcAppraialDetailsByAppraisalId(arcAppraisal.getAppraisalId());
        arcAppraisal.setUpdateTime(DateUtils.getNowDate());
        // 数据判空
        if (StringUtils.isNull(arcAppraisal.getArcAppraialDetailsList())) {
            throw new ServiceException("鉴定明细为空，请检查重试！");
        }
        arcAppraisalMapper.deleteArcAppraialDetailsByAppraisalId(arcAppraisal.getAppraisalId());
        insertArcAppraialDetails(arcAppraisal);
        for (ArcAppraialDetails arcAppraialDetails : arcAppraisal.getArcAppraialDetailsList()) {
            //修改档案信息
            ArcArchive archive = new ArcArchive();
            archive.setArchiveId(arcAppraialDetails.getArchiveId());
            archive.setExpireTime(arcAppraialDetails.getAppraisalAfterTime());
            arcArchiveMapper.updateArcArchive(archive);
        }
        return arcAppraisalMapper.updateArcAppraisal(arcAppraisal);
    }

    /**
     * 批量删除鉴定单
     *
     * @param appraisalIds 需要删除的鉴定单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteArcAppraisalByAppraisalIds(Long[] appraisalIds) {
        arcAppraisalMapper.deleteArcAppraialDetailsByAppraisalIds(appraisalIds);
        return arcAppraisalMapper.deleteArcAppraisalByAppraisalIds(appraisalIds);
    }

    /**
     * 删除鉴定单信息
     *
     * @param appraisalId 鉴定单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteArcAppraisalByAppraisalId(Long appraisalId) {
        arcAppraisalMapper.deleteArcAppraialDetailsByAppraisalId(appraisalId);
        return arcAppraisalMapper.deleteArcAppraisalByAppraisalId(appraisalId);
    }

    /**
     * 新增鉴定详情信息
     *
     * @param arcAppraisal 鉴定单对象
     */
    public void insertArcAppraialDetails(ArcAppraisal arcAppraisal) {
        List<ArcAppraialDetails> arcAppraialDetailsList = arcAppraisal.getArcAppraialDetailsList();
        Long appraisalId = arcAppraisal.getAppraisalId();
        if (StringUtils.isNotNull(arcAppraialDetailsList)) {
            List<ArcAppraialDetails> list = new ArrayList<ArcAppraialDetails>();
            for (ArcAppraialDetails arcAppraialDetails : arcAppraialDetailsList) {
                arcAppraialDetails.setAppraisalId(appraisalId);
                arcAppraialDetails.setCreateTime(DateUtils.getNowDate());
                ArcArchive archiveOld = arcArchiveMapper.selectArcArchiveByArchiveId(arcAppraialDetails.getArchiveId());
                arcAppraialDetails.setAppraisalBeforeTime(archiveOld.getExpireTime());
                list.add(arcAppraialDetails);
            }
            if (list.size() > 0) {
                arcAppraisalMapper.batchArcAppraialDetails(list);
            }
        }
    }
}
